package logic

import (
	"context"
	"errors"
	"goods-guru/goodrpc/goodrpc"
	"goods-guru/goodrpc/internal/svc"
	"goods-guru/model"
	"goods-guru/pkg"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *goodrpc.LoginRequest) (*goodrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var findUser model.User
	model.DB.Debug().Where("account=?", in.Account)
	if findUser.ID != 0 {
		return nil, errors.New("用户已存在,请更改")
	}
	user := model.User{
		Account:  in.Account,
		Password: pkg.MD5(in.Password),
	}

	err := model.DB.Debug().Create(&user).Error
	if err != nil {
		return nil, err
	}
	return &goodrpc.LoginResponse{}, nil
}
